<?php
namespace jy_chatwenxin;
defined('BY_JYA') or exit('error');
class kefu extends \plugin{
	public function getchatid($mid=0){
		$mid=$mid?:$_SESSION['uid'];
		if(!$_SESSION['chatid']){
			$msglog=pdo_get('jy_chatwenxin_msglog',array('chatid'=>0,'mid' =>$mid,'uniacid'=>$_SESSION['uniacid']),array('id','chatid'),'id desc');
			if(!$msglog['chatid']){
				$_SESSION['chatid']=$msglog['id'];
				pdo_update('jy_chatwenxin_msglog',array('chatid'=>$msglog['id'],'ischat'=>1),array('id'=>$msglog['id'],'uniacid'=>$_SESSION['uniacid']));
			}
		}
		return $_SESSION['chatid'];
	}
	public function addchatlog($chatmsg=''){
		$chatmsg = trim($chatmsg);
		if(empty($chatmsg)){
			$return['error'] = 1;
			$return['msg'] = '请输入对话内容！';
			exi($return);
		}
		if(empty($_SESSION['uid'])){
			$return['error'] = 1;
			$return['msg'] = '请先登录会员！';
			exi($return);
		}
		$data=array(
			'uniacid'=>$_SESSION['uniacid'],
			'mid'=>$_SESSION['uid'],
			'chatid'=>$_SESSION['chatid'],
			'askmsg'=>$chatmsg,
			'createtime'=>TIMESTAMP
		);
		pdo_insert('jy_chatwenxin_msglog',$data);
		$logid = pdo_insertid();
		if(!$_SESSION['chatid']){
			$_SESSION['chatid']=$logid;
			pdo_update('jy_chatwenxin_msglog',array('chatid'=>$logid,'ischat'=>1),array('id'=>$logid,'uniacid'=>$_SESSION['uniacid']));
		}
		return $logid;
	}
}